package com.creative.core.page;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.creative.core.uitls.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
 * 分页数据
 *
 * @author ling
 */

public class PageDomain {

    public static final Log logger = LogFactory.get();
    /**
     * 当前记录起始索引
     */
    private Integer pageNum = 1;

    /**
     * 每页显示记录数
     */
    private Integer pageSize = 10;

    /**
     * 排序列
     */
    private String orderByColumn;

    /**
     * 排序的方向desc或者asc
     */
    private String isAsc = "asc";

    /**
     * 分页参数合理化
     */
    private Boolean reasonable = true;


    /**
     * 组合输出   orderByColumn + isAsc
     * 支持多个排序字段
     *
     * @return {@link String }
     * @author ling
     * @date 2024/12/25
     */
    public String getOrderBy() {
        if (StringUtils.isEmpty(orderByColumn)) {
//            logger.info("排序字段 is null");
            return "";
        }
        if (StrUtil.isEmpty(isAsc)) {
//            logger.info("排序方式 is null");
            return "";
        }
        String[] columns = StrUtil.splitToArray(orderByColumn, StrUtil.C_COMMA);
        String[] asc = StrUtil.splitToArray(isAsc, StrUtil.C_COMMA);

        StringBuilder orderBy = new StringBuilder();
        if (columns.length == asc.length && columns.length > 1) {
            for (int i = 0; i < columns.length; i++) {
                String order = "asc".equalsIgnoreCase(asc[i]) ? "asc" : "desc";
                orderBy.append(StringUtils.toUnderScoreCase(columns[i])).append(" ").append(order);
                if (i < columns.length - 1) {
                    orderBy.append(",");
                }
            }
            return orderBy.toString();
        }
//        当两个数组的长度不一致时
        if (columns.length != asc.length) {
            int length = Math.min(asc.length, columns.length);
            for (int i = 0; i < length; i++) {
                String order = "asc".equalsIgnoreCase(asc[i]) ? "asc" : "desc";
                orderBy.append(StringUtils.toUnderScoreCase(columns[i])).append(" ").append(order);
                if (i < length - 1) {
                    orderBy.append(",");
                }
            }
            return orderBy.toString();
        }

//        长度为一的时候
        return StringUtils.toUnderScoreCase(orderByColumn) + " " + isAsc;
    }

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getOrderByColumn() {
        return orderByColumn;
    }

    public void setOrderByColumn(String orderByColumn) {
        this.orderByColumn = orderByColumn;
    }

    public String getIsAsc() {
        return isAsc;
    }

    @Override
    public String toString() {
        return "PageDomain{" +
                "pageNum=" + pageNum +
                ", pageSize=" + pageSize +
                ", orderByColumn='" + orderByColumn + '\'' +
                ", isAsc='" + isAsc + '\'' +
                ", reasonable=" + reasonable +
                '}';
    }

    public void setIsAsc(String isAsc) {

        if (StringUtils.isNotEmpty(isAsc)) {
            // 兼容前端排序类型
            if ("ascending".equals(isAsc)) {
                isAsc = "asc";
            } else if ("descending".equals(isAsc)) {
                isAsc = "desc";
            }

            this.isAsc = isAsc;
        }
    }

    public PageDomain(Integer pageNum, Integer pageSize, String orderByColumn, String isAsc, Boolean reasonable) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.orderByColumn = orderByColumn;
        this.isAsc = isAsc;
        this.reasonable = reasonable;
    }

    public PageDomain() {
    }

    public Boolean getReasonable() {
        if (StringUtils.isNull(reasonable)) {
            return Boolean.TRUE;
        }
        return reasonable;
    }

    public void setReasonable(Boolean reasonable) {
        this.reasonable = reasonable;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;

        if (o == null || getClass() != o.getClass()) return false;

        PageDomain that = (PageDomain) o;

        return new EqualsBuilder().append(pageNum, that.pageNum).append(pageSize, that.pageSize).append(orderByColumn, that.orderByColumn).append(isAsc, that.isAsc).append(reasonable, that.reasonable).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(17, 37).append(pageNum).append(pageSize).append(orderByColumn).append(isAsc).append(reasonable).toHashCode();
    }
}
